package com.Cychat.notice.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Notice;
import com.Cychat.notice.entity.vo.NoticeVO;
import com.Cychat.notice.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    /**
     * 获取所有公告
     * @param
     * @return
     */
    @GetMapping("/getAllNotice")
    public ResultMessage getAllNotice() {
        List<NoticeVO> notices = noticeService.getAllNotice();
        return ResultMessage.success(notices);
    }
    @GetMapping("/getNoticeById/{noticeId}")
    public ResultMessage getNoticeById(@PathVariable("noticeId") Integer noticeId) {
        Notice notice = noticeService.getNoticeById(noticeId);
        return ResultMessage.success(notice);
    }
    @PutMapping("/updateNotice")
    public ResultMessage updateNotice(@RequestBody Notice notice) {
        boolean update = noticeService.updateById(notice);
        return update ? ResultMessage.success() : ResultMessage.error("更新失败");
    }
    /**
     * 存oss
     * @param file
     * @return
     */
    @PostMapping("/updateAvatar")
    public ResultMessage updateAvatar(@RequestParam("file") MultipartFile file) throws IOException {
        //调用service层获取oss返回访问图片链接
        log.info("开始上传图片");
        String url = noticeService.updateAvatar(file);
        if (url!=null){
            return ResultMessage.success(url);
        }
        return ResultMessage.error("上传图片失败");
    }
}

